package com.wms.stock.service.impl;

import com.wms.base.domain.BaseSupplier;
import com.wms.base.mapper.BaseSupplierMapper;
import com.wms.base.mapper.BaseWarehouseMapper;
import com.wms.common.utils.DateUtils;
import com.wms.stock.domain.StockMatLabel;
import com.wms.stock.mapper.StockMatLabelMapper;
import com.wms.stock.service.StockMatLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物料标签Service业务层处理
 */
@Service
public class StockMatLabelServiceImpl implements StockMatLabelService {
    @Autowired
    private StockMatLabelMapper stockMatLabelMapper;

    @Autowired
    private BaseSupplierMapper baseSupplierMapper;

    @Autowired
    private BaseWarehouseMapper warehouseMapper;

    /**
     * 查询物料标签
     *
     * @param labelId 物料标签主键
     * @return 物料标签
     */
    @Override
    public StockMatLabel selectStockMatLabelByLabelId(Long labelId) {
        return stockMatLabelMapper.selectStockMatLabelByLabelId(labelId);
    }

    @Override
    public StockMatLabel selectStockMatLabelByMatCode(String matCode) {
        return stockMatLabelMapper.selectStockMatLabelByMatCode(matCode);
    }

    /**
     * 查询物料标签列表
     *
     * @param stockMatLabel 物料标签
     * @return 物料标签
     */
    @Override
    public List<StockMatLabel> selectStockMatLabelList(StockMatLabel stockMatLabel) {
        return stockMatLabelMapper.selectStockMatLabelList(stockMatLabel);
    }

    /**
     * 查询物料标签列表（弹窗）
     *
     * @param stockMatLabel 物料标签
     * @return 物料标签
     */
    @Override
    public List<StockMatLabel> selectStockMatLabelListDialog(StockMatLabel stockMatLabel) {
        return stockMatLabelMapper.selectStockMatLabelListDialog(stockMatLabel);
    }

    /**
     * 新增物料标签
     *
     * @param stockMatLabel 物料标签
     * @return 结果
     */
    @Override
    public int insertStockMatLabel(StockMatLabel stockMatLabel) {
        String warehouse_name = baseSupplierMapper.selectBaseSupplierBySupplierCode(stockMatLabel.getSupplierCode());
        String warehouse_code = warehouseMapper.selectBaseWarehouseNameByWarehouseName(warehouse_name);
        stockMatLabel.setWarehouseCode(warehouse_code);
        stockMatLabel.setLocationCode(warehouse_code);
        stockMatLabel.setCreateTime(DateUtils.getNowDate());
        stockMatLabel.setUsableQuantity(stockMatLabel.getQuantity());
        stockMatLabel.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getName());
        stockMatLabel.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getName());
        return stockMatLabelMapper.insertStockMatLabel(stockMatLabel);
    }

    /**
     * 修改物料标签
     *
     * @param stockMatLabel 物料标签
     * @return 结果
     */
    @Override
    public int updateStockMatLabel(StockMatLabel stockMatLabel) {
        stockMatLabel.setUpdateTime(DateUtils.getNowDate());
        return stockMatLabelMapper.updateStockMatLabel(stockMatLabel);
    }

    /**
     * 批量删除物料标签
     *
     * @param labelIds 需要删除的物料标签主键
     * @return 结果
     */
    @Override
    public int deleteStockMatLabelByLabelIds(Long[] labelIds) {
        return stockMatLabelMapper.deleteStockMatLabelByLabelIds(labelIds);
    }

    /**
     * 删除物料标签信息
     *
     * @param labelId 物料标签主键
     * @return 结果
     */
    @Override
    public int deleteStockMatLabelByLabelId(Long labelId) {
        return stockMatLabelMapper.deleteStockMatLabelByLabelId(labelId);
    }
}
